#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

#include "Splitter.h"
#include "Buffers.h"
#include "Drives.h"

#pragma code-name("DPCODE");
#pragma rodata-name("DPCODE");

tokenCollection_t* splitString(char *source)
{
	tokenCollection_t *collection = (tokenCollection_t *)malloc(sizeof(tokenCollection_t));
	unsigned char position = 0, length = strlen(source);
	unsigned char inQuotedString = 0;
	
	collection->count = 1;
	collection->tokens[collection->count - 1].text = source;
	
	//printf("\n%s", source);
	for(; position < length; ++position)
	{
		//printf("%u ", position);
		if(source[position] == ' ' && inQuotedString == 0)
		{
			source[position] = '\0';
			//printf("\n%s", collection->tokens[collection->count - 1]);
			collection->tokens[++(collection->count) - 1].text = source + position + 1;
		}
		else if(source[position] == '\"' && inQuotedString == 0)
		{
			inQuotedString = 1;
		}
		else if(source[position] == '\"')
		{
			inQuotedString = 0;
		}
	}
	//printf("\n%s", collection->tokens[collection->count - 1]);
	//printf("\n[%u]", collection->count);

	for(position = 0; position < collection->count; ++position)
	{
		sprintf(arguments, "\nGot here 3: %04X", removeQuotes);
//cgetc();
		removeQuotes(position, collection);
	}

	return collection;
}

void __fastcall__ removeQuotes(unsigned char index, tokenCollection_t *collection)
{
	char *token = collection->tokens[index].text;
	int i = 0, l = strlen(token), j = 0;

	//printf("\nRemoving quotes from [%s]", token);
	strcpy(arguments, token);
	for(; i < l; ++i)
	{
		if(arguments[i]=='\"')
		{
			for(j = i; j < l; ++ j)
			{
				arguments[j] = arguments[j + 1];
			}
			arguments[j] = 0;
		}
	}
	strcpy(token,arguments);
}

bool isPattern(char *source)
{
	return (strchr(source, '*') != NULL ||
		strchr(source, '?') != NULL);
}

parsedPath_t * parsePath(unsigned char index, tokenCollection_t *collection, unsigned char currentDriveIndex)
{
	int i = 0, device = 0, drive = 0, targetDriveIndex = currentDriveIndex;
	char *last, *first;
	static char path[237], file[17], temp[256];
	parsedPath_t *results = NULL;

	memset(temp, 0, sizeof(temp));
	sscanf(collection->tokens[index].text, "%d#%d:%s", &device, &drive, temp);
	if(strlen(temp) == 0 && device == 0) 
	{
		strcpy(temp, collection->tokens[index].text);
	}

	last = strrchr(temp, ':');
	first = strchr(temp, ':');

	memset(path, 0, sizeof(path));
	memset(file, 0, sizeof(file));

	if(first != NULL && last != first)
	{
		// has drive separator and path separator
		strncpy(path, first + 1, last - first);
		strncpy(file, last + 1, 16);
	}
	else if(first != NULL && last == first && strchr(temp, '#') != NULL)
	{
		// has drive separator but no path separator
		strncpy(path, temp, 16);
	}
	else if(first != NULL && last == first && strchr(temp, '#') == NULL)
	{
		// has no drive separator but has path separator
		strncpy(path, temp, first - temp);
		strncpy(file, first + 1, 16);
	}
	else 
	{
		// has no drive separator or path separator
		strncpy(path, temp, sizeof(path));
	}

	results = (parsedPath_t *)malloc(sizeof(parsedPath_t));
	memset(results, 0, sizeof(results));

	if(device == 0)
	{
		// No device specified
		device = drives[currentDriveIndex].device;
		drive = drives[currentDriveIndex].drive;
	} else {
		targetDriveIndex = getDrive(device);
	}

	results->device = device;
	results->drive = drive;

	if(drives[targetDriveIndex].path[0] != 0 && path[0] != '/' && path[0] != 0)
	{
		sprintf(results->path, "%s%s%c", (drives[targetDriveIndex].path), path, strlen(drives[targetDriveIndex].path) > 2 ? '/' : 0);
	}
	else
	{
		strcpy(results->path, path);
	}

	if(results->path[strlen(results->path) - 1] != '/') strcat(results->path, "/");

	strcpy(results->file, file);

	return results;
}
